package com.chen.manager.controller;

import com.chen.manager.mapper.SysMenuMapper;
import com.chen.manager.service.SysMenuService;
import com.chen.manager.service.SysUserService;
import com.chen.manager.service.ValidateCodeService;
import com.chen.model.dto.system.LoginDto;
import com.chen.model.entity.system.SysMenu;
import com.chen.model.entity.system.SysUser;
import com.chen.model.vo.common.Result;
import com.chen.model.vo.common.ResultCodeEnum;
import com.chen.model.vo.system.LoginVo;
import com.chen.model.vo.system.SysMenuVo;
import com.chen.model.vo.system.ValidateCodeVo;
import com.chen.util.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cgj
 * @date 2/6/25 10:49 AM
 */
@RestController
@RequestMapping("/admin/system/index")
//@CrossOrigin
//http://localhost:8501/admin/system/index/login
public class IndexController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private SysMenuService sysMenuService;

    /**
     *  动态菜单 用户登录后触发
     *  查询用户所拥有的菜单
     * @return
     */
    @GetMapping("/menus")
    public Result<List<SysMenuVo>> getMenu(){
        //可以从threadLocal中 拿取用户的id ,什么时候用什么时候在拿
        List<SysMenuVo> sysMenuVoList=sysMenuService.getMenus();
        return Result.build(sysMenuVoList,ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户退出
     */
    @GetMapping("logout")
    public Result logout (@RequestHeader(name = "token") String token){
        sysUserService.logout(token);
        return  Result.build(null, ResultCodeEnum.SUCCESS);
    }


    /**   /admin/system/index/userinfo
     * 查看用户信息
     * @return
     */
    @GetMapping("/userinfo")
//    public Result<SysUser> getUserInfo(@RequestHeader(name = "token" ) String token){
    public Result<SysUser> getUserInfo(){
//        SysUser sysUser=  sysUserService.getUserINfo(token);
        SysUser sysUser = AuthContextUtil.get();
        return Result.build(sysUser,ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户登录
     * @param
     * @return
     */
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        LoginVo loginVo=sysUserService.login(loginDto);
        return  Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }
    /**
     * /admin/system/index/generateValidateCode
     */
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo= validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo,ResultCodeEnum.SUCCESS);
    }
}
